<%-- 
    Document   : editHus
    Created on : 10.okt.2012, 17:51:19
    Author     : 500134
--%>
<%@page import="java.util.ArrayList"%>
<%@include file="WEB-INF/jspf/header.jspf"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%
ArrayList<String> objektNavn;
ArrayList<String> varmekilde;
ArrayList<String> svarmekilde;
ArrayList<String> objektIHus;
objektNavn = db.DatabaseKobling.getHouseholdArticle(request.getSession().getAttribute("username").toString()); 
varmekilde = db.DatabaseKobling.getHeatsources();
String hus = request.getParameter("hName");
int id = db.DatabaseKobling.getStructureId(hus);
objektIHus = db.DatabaseKobling.getStructureFurniture(id);
String eKlasse = db.DatabaseKobling.getStructureEnergyclass(hus);
int areal = db.DatabaseKobling.getStructureArea(hus);
int alder = db.DatabaseKobling.getStructureAge(hus);
String primary = db.DatabaseKobling.getStructurePHeatsource(hus);
String sekundær = db.DatabaseKobling.getStructureSHeatsource(hus);
svarmekilde = db.DatabaseKobling.getsHeatsources(primary);
int prosent = db.DatabaseKobling.getStructurePercent(hus);
int antall = db.DatabaseKobling.getStructureNumInc(hus);
%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script>
            function sHeatsource()
            {
                var xmlhttp;
                var heatsource = document.getElementById("pHeatsource").value
                if (window.XMLHttpRequest)
                {// code for IE7+, Firefox, Chrome, Opera, Safari
                    xmlhttp=new XMLHttpRequest();
                }
                else
                {// code for IE6, IE5
                    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlhttp.onreadystatechange=function()
                {
                    if (xmlhttp.readyState==4 && xmlhttp.status==200)
                    {
                        document.getElementById("sHeatsource").innerHTML=xmlhttp.responseText;
                    }
                }
                xmlhttp.open("GET","heatsource.jsp?pname="+heatsource,true);
                xmlhttp.send();
            }
            
            function add()
            {
                var option = document.getElementById("obj").value +" "+ document.getElementById("numb").value
                var newOption = document.createElement('option')
                var text = document.createTextNode(option)
                newOption.appendChild(text)
                newOption.value = option
                document.getElementById('nyobj').appendChild(newOption)  
            }
            function back()
            {
                var element = document.getElementById("nyobj")    
                element.remove(element.selectedIndex)   
            }
            
            function selectAllOptions(selStr)
            {
                var selObj = document.getElementById(selStr);
                for (var i=0; i<selObj.options.length; i++) {
                    selObj.options[i].selected = true;
                }
            }
            
            function isEmpty(str) {
        for (var intLoop = 0; intLoop < str.length; intLoop++)
        if (" " != str.charAt(intLoop))
        return false;
        return true;
        }
            
        function checkRequired(f) {
        var strError = "";
        for (var intLoop = 0; intLoop < f.elements.length; intLoop++)
        if (null!=f.elements[intLoop].getAttribute("required")) 
        if (isEmpty(f.elements[intLoop].value))
        strError += "  " + f.elements[intLoop].name + "\n";
        if ("" != strError) {
        alert("Required data is missing:\n" + strError);
        return false;
        } else
        return true}
     
        function runAll(){
            selectAllOptions('nyobj');
            checkRequired(this);
        }
        </script>
        <title>JSP Page</title>
    </head>
    <body>
      <h2>Edit bygning</h2>
        <div class="ny_bygning_area">
            <div class="ny_bygning_box">
                <form action="EditHusServlet" onsubmit="runAll()">&nbsp;
                    <input type="hidden" name="husId" value="<%=id%>">
            Navn: <input type="text" name="profilnavn" value="<%=hus%>" required/><br/><br/>
            Areal: <input type="number" name="areal" value="<%=areal%>" min="0" value="0" required/><br/><br/>
            Alder: <input type="number" name="alder" value="<%=alder%>" min="0" value="0" required/><br/><br/>
            Energi klasse <select name="energiclass">
                <option selected="selected"><%=eKlasse%></option>
                <option type='separator' disabled='disabled'>--</option>
                             <option>A</option>
                             <option>B</option>
                             <option>C</option>
                             <option>D</option>
                             <option>E</option>
                             <option>F</option>
                          </select><br/><br/>
                          Primær varmekilde: <select id="pHeatsource" name="PHeatsource" onchange="sHeatsource()">
               <% out.print("<option selected='selected'>"+primary+"</option>");
               out.print("<option type='separator' disabled='disabled'>------------</option>");
               for (int i = 0; i < varmekilde.size(); i++)                              
                out.print("<option>" + varmekilde.get(i) + "</option>");  %>
                               </select><br/>
                               
            
            <div id="sHeatsource">
                Sekundær varmekilde:
                <select name="SHeatsource">
                <%
                    out.print("<option selected='selected'>"+sekundær+"</option>");
                    out.print("<option type='separator' disabled='disabled'>------------</option>");
                    for (int i = 0; i < svarmekilde.size(); i++) {
                        out.print("<option>" + svarmekilde.get(i) + "</option>");
                    }
                %>
            </select></div><br/>
            Antall lysenheter: <input type="number" name="antall" size="7" value="<%=antall%>" min="0" value="0" required/><br/><br/>

            Andel glødepærer: <input type="range" name="andel" min="0" max="100" value="<%=prosent%>" step="1" onchange="showValue(this.value)"/>
            <span id="range"><%=prosent%></span>
            <script type="text/javascript">
            function showValue(newValue)
            {
                document.getElementById("range").innerHTML=newValue;
            }
            </script><br/><br/>
            Objekter: <select id="obj" size="5" >
                        <%  for (int i = 0; i < objektNavn.size(); i++)
                out.print("<option seleced='selected'>" + objektNavn.get(i) + "</option>"); %>
                      </select>
                      Antall: <input id="numb" type="number" name="antall" size="5" min="1" value="1"/>          
            <input type="button" onclick="back()" value="<-"/> 
            <input type="button" onclick="add()" value="->"/>
            <select id="nyobj" name="selected" size="5">
                        <% for (int i = 0; i < objektIHus.size(); i++)                              
                out.print("<option>" + objektIHus.get(i) + "</option>");  %>            
            </select><br/><br/>
            <input type="submit" value="Lagre"/>
            <a href="mineBygninger.jsp">Tilbake</a>
        </form>
       </div>
      </div>
    </body>
</html>
<%@include file="WEB-INF/jspf/footer.jspf"%>